package com.gitee.wsl.func.procedure.sample

import com.gitee.wsl.func.procedure.Procedure

/**
 * ChainedProcedure allows a developer to chain together procedure to be executed in sequence.
 */
class ChainedProcedure<T> : Procedure<T> {
    private val procedures =  mutableListOf<Procedure<in T>>()

    fun addProcedure(procedure: Procedure<in T>) {
        this.procedures.add(procedure)
    }

    override fun value(each: T) {
        val size: Int = this.procedures.size
        for (i in 0..<size) {
            this.procedures[i].value(each)
        }
    }

    override fun toString(): String {
        return "ChainedProcedure.with(" + this.procedures + ')'
    }

    companion object {

        fun <E> with(procedure1: Procedure<in E>, procedure2: Procedure<in E>): ChainedProcedure<E> {
            val chainedProcedure = ChainedProcedure<E>()
            chainedProcedure.addProcedure(procedure1)
            chainedProcedure.addProcedure(procedure2)
            return chainedProcedure
        }
    }
}
